<template>
	<view class="my-page">
		<u-navbar leftIconColor="#000" :autoBack="true" :placeholder="true" bgColor="rgba(255,255,255,0.0)">
			<view slot="center" class="nav-title">
				总收益
			</view>
		</u-navbar>
		<view class="head-box">
			<view class="head-top">
				<view class="head-left">
					<view class="head-text">
						可兑换收益
					</view>
					<view class="head-num">
						{{userinfo.all_assets|| 0}}
					</view>
				</view>
			</view>
			<view class="income">
				<view class="income-item">
					<view class="income-text">
						货款收益
					</view>
					<view class="income-num">
						{{userinfo.zzReward|| 0}}
					</view>
				</view>
				<view class="income-item">
					<view class="income-text">
						推荐收益
					</view>
					<view class="income-num">
						{{userinfo.profit_recom|| 0}}
					</view>
				</view>
				<view class="income-item">
					<view class="income-text">
						团队收益
					</view>
					<view class="income-num">
						{{userinfo.profit_team|| 0}}
					</view>
				</view>
				<!-- <view class="income-item">
					<view class="income-text">
						收益转存
					</view>
					<view class="income-num">
						{{userinfo.profit_deposit|| 0}}
					</view>
				</view> -->
			</view>
			<view class="gold">
				<image src="../../../static/home/gold.png" mode=""></image>
			</view>
			<view class="btn-box">
				<view class="head-btn" @click="ppshow = true">
					兑换积分
				</view>
				<view class="head-btn" @click="show = true">
					兑换商品
				</view>
			</view>
			<view style="height: 90rpx;"></view>
			<view class="price">
				<view class="price-item">
					<view class="price-text">
						昨日收益
					</view>
					<view class="price-num">
						{{ userinfo.yesterdayReward || 0 }}
					</view>
				</view>
				<view class="price-item">
					<view class="price-text">
						今日收益
					</view>
					<view class="price-num">
						{{ userinfo.todayReward || 0 }}
					</view>
				</view>
				<view class="price-item">
					<view class="price-text" style="color: #FD586C;">
						累计收益
					</view>
					<view class="price-num">
						{{ userinfo.allReward || 0 }}
					</view>
				</view>
			</view>
		</view>
		<view style="height: 80rpx;"></view>
		<view class="box">
			<view class="tabs">
				<view :class="current == index?'active-item': 'tabs-item'" class="tabs-item"
					v-for="(item,index) in tabsList" :key="index" @click="cutTab(index,item.type)">
					{{item.text}}
				</view>
			</view>
			<view v-if="accountLoglist.length > 0">
				<view class="record-item" v-for="(item,index) in accountLoglist" :key="index">
					<view class="record-left">
						<view class="record-text">
							{{item.message}}
						</view>
						<view class="record-time">
							{{item.time}}
						</view>
					</view>
					<view class="record-num" v-if="item.num >0">
						+{{item.num}}
					</view>
					<view class="record-num" v-else>
						{{item.num}}
					</view>
				</view>
				<u-loadmore :status="status" />
			</view>
			<u-empty v-else mode="car" icon="../../../static/home/none.png" text="暂无数据" marginTop="50">
			</u-empty>
		</view>
		<u-popup z-index="11" :show="show" @close="close" mode="center" round="20">
			<view class="pop">
				<view class="pop-title">
					商品兑换
				</view>
				<view class="pop-item">
					<view class="pop-text">
						选择产品
					</view>
					<view class="pop-input" @click="goodsShow = true">
						<u-input placeholder="请选择" border="none" readonly v-model="diamond">
							<template slot="suffix">
								<u-icon name="arrow-down" color="#666666" size="16"></u-icon>
							</template>
						</u-input>
					</view>
				</view>
				<view class="pop-item">
					<view class="pop-text">
						出售价格
					</view>
					<view class="pop-input">
						<u--input placeholder="请输入出售价格" border="none" v-model="price"></u--input>
					</view>
				</view>
				<view class="pop-item">
					<view class="pop-text">
						出售数量
					</view>
					<view class="pop-input">
						<u--input placeholder="请输入出售数量" border="none" v-model="num"></u--input>
					</view>
				</view>
				<view class="pop-item">
					<view class="pop-text">
						支付密码
					</view>
					<view class="pop-input">
						<u--input type="number" placeholder="请输入支付密码" border="none" v-model="password"></u--input>
					</view>
				</view>
				<view class="pop-btn">
					<u-button shape="circle" size="large" color="linear-gradient(90deg, #FD5674, #FF6448)"
						type="primary" text="确认兑换" @click="exchange"></u-button>
				</view>
			</view>
		</u-popup>
		<u-popup z-index="11" :show="ppshow" @close="ppclose" mode="center" round="20">
			<view class="pop">
				<view class="pop-title">
					兑换积分
				</view>
				<view class="pop-item">
					<view class="pop-text">
						兑换数量
					</view>
					<view class="pop-input">
						<u--input placeholder="请输入兑换数量" border="none" v-model="num"></u--input>
					</view>
				</view>
				<view class="pop-item">
					<view class="pop-text">
						支付密码
					</view>
					<view class="pop-input">
						<u--input type="number" placeholder="请输入支付密码" border="none" v-model="password"></u--input>
					</view>
				</view>
				<view class="pop-btn">
					<u-button shape="circle" size="large" color="linear-gradient(90deg, #FD5674, #FF6448)"
						type="primary" text="确认兑换" @click="ppchange"></u-button>
				</view>
			</view>
		</u-popup>
		<u-picker keyName="name" :show="goodsShow" :columns="columns" @cancel="goodsShow = false"
			@confirm="confirm"></u-picker>
	</view>
</template>

<script>
	import CryptoJS from 'crypto-js';
	export default {
		data() {
			return {
				tabsList: [{
						text: '货款收益',
						type: 8
					},
					{
						text: '推荐收益',
						type: 4
					},
					{
						text: '团队收益',
						type: 5
					}
				],
				current: 0,
				show: false,
				diamond: '',
				price: '',
				num: '',
				password: '',
				accountLoglist: [],
				page: 1,
				userinfo: {},
				type: 8,
				goodsShow: false,
				columns: [],
				goods_id: '',
				min_price: '',
				max_price: '',
				status: 'loadmore',
				ppshow: false
			}
		},
		onLoad() {
			this.postaccountLog()
			this.getGoods()
		},
		onReachBottom() {
			this.page++
			this.status = 'loading';
			this.postaccountLog()
		},
		methods: {
			ppchange() {
				if (this.num == '') {
					uni.showToast({
						title: '请输入兑换数量！',
						icon: 'none'
					})
					return
				}
				if (this.password == '') {
					uni.showToast({
						title: '请输入支付密码！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				let params = {
					num: this.num,
					password: this.md5Encrypt(this.password)
				}
				this.$Request({
					method: 'POST',
					url: '/api/Mutualaidorder/exchangeBalance',
					data: params,
					ismsg: true,
				}).then(res => {
					console.log(res)
					uni.hideLoading()
					if (res.code == 1) {
						this.num = ''
						this.password = ''
						this.ppshow = false
						this.postaccountLog()
					}
				})
			},
			ppclose() {
				this.ppshow = false
			},
			md5Encrypt(text) {
				return CryptoJS.MD5(text).toString();
			},
			postaccountLog() {
				let params = {
					page: this.page,
					type: this.type
				}
				this.$Request({
					method: 'POST',
					url: '/api/mutualaidorder/accountLog',
					data: params,
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.userinfo = res.data.assets
						if (res.data.list.length < 10) {
							this.status = 'nomore';
						}
						if (this.page == 1) {
							this.accountLoglist = res.data.list
						} else {
							this.accountLoglist = [...this.accountLoglist, ...res.data.list]
						}

					}
				})

			},
			cutTab(index, type) {
				this.type = type
				this.page = 1
				this.current = index
				this.postaccountLog()
			},
			close() {
				this.show = false
			},
			confirm(e) {
				this.diamond = e.value[0].name
				this.goods_id = e.value[0].id
				this.min_price = e.value[0].min_price
				this.max_price = e.value[0].max_price
				this.goodsShow = false
			},
			getGoods() {
				this.$Request({
					method: 'POST',
					url: '/api/mutualaidorder/exchangeProfit',
					data: {},
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.columns.push(res.data.purchase)
					}
				})
			},
			exchange() {
				if (this.diamond == '') {
					uni.showToast({
						title: '请选择产品！',
						icon: 'none'
					})
					return
				}
				if (this.price == '') {
					uni.showToast({
						title: '请输入出售价格！',
						icon: 'none'
					})
					return
				}
				if (this.num == '') {
					uni.showToast({
						title: '请输入出售数量！',
						icon: 'none'
					})
					return
				}
				if (this.password == '') {
					uni.showToast({
						title: '请输入支付密码！',
						icon: 'none'
					})
					return
				}
				if (this.price < this.min_price) {
					uni.showToast({
						title: '价格不可小于' + this.min_price,
						icon: 'none'
					})
					return
				}
				if (this.price > this.max_price) {
					uni.showToast({
						title: '价格不可大于' + this.max_price,
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				let params = {
					price: this.price,
					purchase_id: this.goods_id,
					num: this.num,
					password: this.md5Encrypt(this.password)
				}
				this.$Request({
					method: 'POST',
					url: '/api/mutualaidorder/exchangePurchase',
					data: params,
					ismsg: true,
				}).then(res => {
					console.log(res)
					uni.hideLoading()
					if (res.code == 1) {
						this.diamond = ''
						this.price = ''
						this.goods_id = ''
						this.num = ''
						this.password = ''
						this.show = false
						this.postaccountLog()
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #fafafa;
	}
</style>
<style lang="scss" scoped>
	.my-page {
		background-image: url('../../../static/home/home-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 500rpx;
		min-height: 80vh;
		padding: 30rpx;

		.nav-title {
			color: #000;
		}

		.pop {
			width: 680rpx;
			padding: 30rpx;

			.pop-title {
				text-align: center;
				font-size: 32rpx;
				color: #000;
			}

			.pop-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 30rpx;

				.pop-text {
					font-size: 28rpx;
					color: #000;
				}

				.pop-input {
					width: 78%;
					padding: 15rpx 30rpx;
					border-radius: 30rpx;
					background-color: #F7F5F6;
				}
			}

			.pop-btn {
				margin-top: 40rpx;
			}
		}

		.head-box {
			background-image: url('../../../static/home/iconme-bg.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
			padding: 30rpx;
			position: relative;

			.head-top {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.head-left {
					color: #fff;

					.head-text {
						font-size: 24rpx;
					}

					.head-num {
						font-size: 48rpx;
						font-weight: bold;
						margin-top: 10rpx;
					}
				}

			}

			.income {
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
				padding: 0 30rpx;
				.income-item {
					text-align: center;
					color: #fff;

					.income-text {
						font-size: 26rpx;
					}

					.income-num {
						font-size: 32rpx;
						font-weight: bold;
						margin-top: 5px;
					}
				}
			}

			.gold {
				position: absolute;
				right: 20rpx;
				top: -10rpx;

				image {
					width: 180rpx;
					height: 120rpx;
				}
			}

			.btn-box {
				display: flex;
				justify-content: space-around;
				margin-top: 40rpx;

				.head-btn {
					width: 45%;
					text-align: center;
					background-color: rgba(255, 255, 255, 0.2);
					padding: 20rpx;
					font-size: 24rpx;
					color: #fff;
					border-radius: 40rpx;
				}
			}
		}

		.price {
			margin: 0 auto;
			width: 90%;
			position: absolute;
			left: 0;
			right: 0;
			bottom: -50rpx;
			background-color: #fff;
			padding: 30rpx 30rpx 20rpx;
			border-radius: 30rpx;
			display: flex;
			justify-content: space-between;
			box-shadow: 0rpx 3rpx 32rpx 1rpx rgba(0, 0, 0, 0.08);


			.price-item {
				text-align: center;

				.price-num {
					color: #000000;
					font-size: 36rpx;
					font-weight: bold;
					margin-top: 10rpx;
				}

				.price-text {
					color: #999999;
					font-size: 28rpx;
				}
			}
		}

		.tabs {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 10rpx;
			background-color: #f5f6f9;
			padding: 15rpx 10rpx;
			border-radius: 10rpx;

			.tabs-item {
				font-size: 28rpx;
				font-weight: 500;
				color: #cfcfd1;
				padding: 15rpx 40rpx;
			}

			.active-item {
				color: #000000;
				padding: 15rpx 40rpx;
				border-radius: 10rpx;
				background-color: #fff;
			}
		}

		.box {
			padding: 20rpx;
			border-radius: 30rpx;
			background-color: #fff;
		}

		.record-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 30rpx;
			background-color: #fff;
			border-radius: 20rpx;
			border-bottom: 1rpx solid #dddddd;

			.record-left {
				.record-text {
					font-size: 30rpx;
					color: #333;
				}

				.record-time {
					font-size: 24rpx;
					color: #999999;
					margin-top: 5rpx;
				}
			}

			.record-num {
				font-size: 32rpx;
				color: #FD586C;
				font-weight: bold;
			}
		}
	}
</style>